package com.example.fragmentTab;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.viewpageindicator.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TabViewPagerActivity extends FragmentActivity implements View.OnClickListener{
    private LinearLayout mWeixinBar;
    private LinearLayout mFrdBar;
    private LinearLayout mAddressBar;
    private LinearLayout mSettingBar;
    private List<Fragment> mFragmentList = new ArrayList<>();

    @Bind(R.id.tab_view_pager)
    public ViewPager mTabViewPager;

    @Bind(R.id.bar_weixin_img)
    public ImageButton mWeixinImg;
    @Bind(R.id.bar_frd_img)
    public ImageButton mFrdImg;
    @Bind(R.id.bar_address_img)
    public ImageButton mAddressImg;
    @Bind(R.id.bar_setting_img)
    public ImageButton mSettingImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_view_pager);
        ButterKnife.bind(this);

        initView();
    }


    private void initView() {
        mWeixinBar = (LinearLayout) findViewById(R.id.bar_weixin_view);
        mFrdBar = (LinearLayout) findViewById(R.id.bar_frd_view);
        mAddressBar = (LinearLayout) findViewById(R.id.bar_address_view);
        mSettingBar = (LinearLayout) findViewById(R.id.bar_setting_view);

        mWeixinBar.setOnClickListener(this);
        mFrdBar.setOnClickListener(this);
        mAddressBar.setOnClickListener(this);
        mSettingBar.setOnClickListener(this);

        Fragment weixinFragment = new WeixinFragment();
        Fragment frdFragment = new FrdFragment();
        Fragment addressFragment = new AddressFragment();
        Fragment settingFragment = new SettingFragment();

        mFragmentList.add(weixinFragment);
        mFragmentList.add(frdFragment);
        mFragmentList.add(addressFragment);
        mFragmentList.add(settingFragment);

        mTabViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        });

        mTabViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.bar_weixin_view:
                setFragment(0);
                break;
            case R.id.bar_frd_view:
                setFragment(1);
                break;
            case R.id.bar_address_view:
                setFragment(2);
                break;
            case R.id.bar_setting_view:
                setFragment(3);
                break;
            default:
                break;
        }
    }


    public void setFragment(int id){
        setTab(id);
        mTabViewPager.setCurrentItem(id);
    }

    private void setTab(int id ){
        resetImg();
        switch (id) {
            case 0:
                mWeixinImg.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                mFrdImg.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                mAddressImg.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                mSettingImg.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
    }

    private void resetImg() {
        mWeixinImg.setImageResource(R.drawable.tab_weixin_normal);
        mFrdImg.setImageResource(R.drawable.tab_find_frd_normal);
        mAddressImg.setImageResource(R.drawable.tab_address_normal);
        mSettingImg.setImageResource(R.drawable.tab_settings_normal);
    }
}
